﻿using UnityEngine;
using UnityEngine.UI;
using System.Collections;

public class GameEnd : MonoBehaviour {

    public Button again;
    public Button end;
    public GameObject restartItem;
    public GameObject content;

    private LayoutElement _itemLayoutElement;

	// Use this for initialization
	void Start () {

        _itemLayoutElement = restartItem.GetComponent<LayoutElement>();

        if (ScrollRectControl.Instance.restartList != null)
        {
            AddRestartItem();
        }
	}
	
	// Update is called once per frame
	void Update () {
	
	}

    public void Again()
    {
        Application.LoadLevel(0);
        PlayerPrefs.DeleteAll();
        Time.timeScale = 1;
        this.gameObject.SetActive(false);
    }

    public void End()
    {
        Application.Quit();
    }

    public void Back()
    {
        Time.timeScale = 1;
        this.gameObject.SetActive(false);
    }
    
    void AddRestartItem()
    {
        foreach (TextInfo ti in ScrollRectControl.Instance.restartList)
        {
            if (restartItem == null || content == null)
            {
                return;
            }
            GameObject inst = GameObject.Instantiate(restartItem) as GameObject;
            inst.transform.SetParent(content.transform, false);
            inst.transform.localPosition = Vector3.zero;
            inst.transform.localScale = Vector3.one;
            inst.SetActive(true);
            SetGridHeight(1);

            RestartInfo ri = inst.GetComponent<RestartInfo>();
            ri.SaveTextInfo(ti);
            Text restartText = inst.GetComponentInChildren<Text>();

            if (ti.itemType == 1)
            {
                restartText.text = ti.system;
            }
            else
            {
                restartText.text = ti.text1;
            }

            Button restartButton = inst.GetComponentInChildren<Button>();

            if (restartButton != null)
            {
                restartButton.onClick.AddListener(delegate()
                {
                    this.Restart(inst);
                });
            }
        }
    }

    void SetGridHeight(int num)
    {
        VerticalLayoutGroup VerticalLG = content.GetComponent<VerticalLayoutGroup>();

        float childCount = content.transform.childCount;

        float height = ((childCount + num - 1) / num) * _itemLayoutElement.preferredHeight + 3.0f;
        //height += (((childCount + num - 1) / num) - 1) * g.spacing.y;
        content.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, height);
    }

    //重新开始
    public void Restart(GameObject g)
    {
        //隐藏结束界面
        this.gameObject.SetActive(false);

        RestartInfo info = g.GetComponent<RestartInfo>();

        if (PlayerPrefs.HasKey("GameFlow"))
        {
            //重新添加数组，删除Key
            int[] temp = PlayerPrefsX.GetIntArray("GameFlow");
            ScrollRectControl.Instance.gameFlowArray.Clear();
            for (int i = 0; i < temp.Length; i++)
            {
                if (temp[i] <= info.textInfo.id)
                {
                    ScrollRectControl.Instance.gameFlowArray.Add(temp[i]);
                }
                //找到数组里比传过来的ID大的选择KEY值，然后删掉
                if (temp[i] >= info.textInfo.id)
                {
                    string chooseKey = "playerChoose" + info.textInfo.id.ToString();
                    if (PlayerPrefs.HasKey(chooseKey))
                        PlayerPrefs.DeleteKey(chooseKey);
                }
            }
            int[] ary = (int[])ScrollRectControl.Instance.gameFlowArray.ToArray(typeof(int));
            PlayerPrefsX.SetIntArray("GameFlow", ary);
            //删除原来的，并且调整高度
            for (int i = 0; i < ScrollRectControl.Instance.content.transform.childCount; i++)
            {
                GameObject go = ScrollRectControl.Instance.content.transform.GetChild(i).gameObject;
                Destroy(go);
            }
            ScrollRectControl.Instance.content.GetComponent<RectTransform>().SetSizeWithCurrentAnchors(RectTransform.Axis.Vertical, 3f);
            //现有数组中，大于数据后面的选择，全部要删除
            //if (info.textInfo.id == temp[temp.Length - 1])
            //{
            //    string chooseKey = "playerChoose" + info.textInfo.id.ToString();
            //    PlayerPrefs.DeleteKey(chooseKey);
            //}
            
            //开始重新生成
            PlayerInfo pi = GameObject.Find("PlayerInfo").GetComponent<PlayerInfo>();
            pi.CurrenHealth = 100;
            Time.timeScale = 1;
            ScrollRectControl.Instance.PlayerComeBack(PlayerPrefsX.GetIntArray("GameFlow"));
        }
        
    }
}
